﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ButtonTree2
{
    /// <summary>
    /// 功能：
    /// 1、对于控件中有多个子控件，方便判断整体控件上鼠标悬停状态
    /// 2、方便操作多个子控件的Click事件
    /// </summary>
    public class MouseEventStrategy
    {
        public MouseEventStrategy()
        {
            CountMouseHover = 0;
            IsMouseHover = false;
        }


        public int CountMouseHover
        {
            get;
            private set;
        }

        public bool IsMouseHover
        {
            get;
            private set;
        }

        public bool IsMouseDown
        {
            get
            {
                return this._bMouseDown;
            }
        }

        private void OnMouseEnter(object sender, EventArgs e)
        {
            CountMouseHover = Math.Max(0, CountMouseHover);
            ++CountMouseHover;
            if (CountMouseHover > 0 && !IsMouseHover)
            {
                _bMouseDown = false;
                IsMouseHover = true;
                if (MouseHoverStateChangeAction != null)
                {
                    MouseHoverStateChangeAction(IsMouseHover);
                }
            }

        }

        private void OnMouseLeave(object sender, EventArgs e)
        {
            --CountMouseHover;
            if (CountMouseHover == 0 && IsMouseHover)
            {
                IsMouseHover = _bMouseDown;
                _bMouseDown = false;
                if (MouseHoverStateChangeAction != null)
                {
                    MouseHoverStateChangeAction(IsMouseHover);
                }
            }
        }

//         private void OnClick(object sender, EventArgs e)
//         {
//             if (ClickEventHandler != null)
//             {
//                 ClickEventHandler(sender, e);
//             }
//         }

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (MouseMoveEventHandler != null)
            {
                MouseMoveEventHandler(sender, e);
            }
        }

        private bool _bMouseDown = false;
        private void OnMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _bMouseDown = true;
                if (MouseLeftButtonDown != null)
                {
                    MouseLeftButtonDown(this, EventArgs.Empty); // 调用鼠标左键按下委托
                }
            }
        }

        private void OnMouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) // 鼠标已按下
            {
                if (MouseLeftButtonUp != null)
                {
                    MouseLeftButtonUp(this, EventArgs.Empty);   // 调用鼠标左键弹出委托
                }
                if (_bMouseDown) //鼠标在控件范围内部
                {
                    Control control = sender as Control;
                    if (control != null)
                    {
                        if (control.ClientRectangle.Contains(e.Location))
                        {
                            if (ClickEventHandler != null)
                            {
                                ClickEventHandler(sender, e);
                            }
                        }
                    }
                }
                _bMouseDown = false;
            }
        }

        private void OnLostFocus(object sender, EventArgs e)
        {
            CountMouseHover = 0;
            _bMouseDown = false;
            IsMouseHover = false;
            if (MouseHoverStateChangeAction != null)
            {
                MouseHoverStateChangeAction(IsMouseHover);
            }
        }

        public void AddControlMouseEvent(Control control)
        {
            control.MouseEnter += new EventHandler(OnMouseEnter);
            control.MouseLeave += new EventHandler(OnMouseLeave);
            //control.Click += new EventHandler(OnClick);
            control.MouseMove += new MouseEventHandler(OnMouseMove);
            control.MouseDown += new MouseEventHandler(OnMouseDown);
            control.MouseUp += new MouseEventHandler(OnMouseUp);
            control.LostFocus += new EventHandler(OnLostFocus);
        }

        public void RemoveControlMouseEvent(Control control)
        {
            control.MouseEnter -= new EventHandler(OnMouseEnter);
            control.MouseLeave -= new EventHandler(OnMouseLeave);
            //control.Click -= new EventHandler(OnClick);
            control.MouseMove -= new MouseEventHandler(OnMouseMove);
            control.MouseDown -= new MouseEventHandler(OnMouseDown);
            control.MouseUp -= new MouseEventHandler(OnMouseUp);
            control.LostFocus -= new EventHandler(OnLostFocus);
        }

        public Action<bool> MouseHoverStateChangeAction = null;
        public EventHandler ClickEventHandler = null;
        public MouseEventHandler MouseMoveEventHandler = null;
        public EventHandler MouseLeftButtonDown = null; // 鼠标左键按下委托
        public EventHandler MouseLeftButtonUp = null;   // 鼠标左键弹出委托
    }
}
